package com.chenbei.schedule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.common.domain.vo.Pagination;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.schedule.domain.ScheduleJob;
import com.chenbei.schedule.domain.ScheduleJobLog;
import com.chenbei.schedule.service.ScheduleJobLogService;
import com.chenbei.schedule.service.ScheduleJobService;
import com.chenbei.schedule.vo.req.ScheduleJobLogReq;
import com.chenbei.schedule.vo.req.ScheduleJobReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleJobLogService scheduleJobLogService;

    @Autowired
    private ScheduleJobService scheduleJobService;

    @PostMapping("add")
    public ApiResponse<Object> add(@Validated ScheduleJob scheduleJob) {
        scheduleJobService.add(scheduleJob);
        return ApiResponseUtil.success(true);
    }

    @PostMapping("delete/{jobId}")
    public ApiResponse<Object> delete(@PathVariable Long jobId) {
        scheduleJobService.delete(jobId);
        return ApiResponseUtil.success(true);
    }

    @PostMapping("update")
    public ApiResponse<Object> update(@Validated ScheduleJob scheduleJob) {
        scheduleJobService.update(scheduleJob);
        return ApiResponseUtil.success(true);
    }

    @GetMapping("pageList")
    public ApiResponse<Pagination<ScheduleJob>> pageList(ScheduleJobReq req) {
        Page<ScheduleJob> scheduleJobPage = scheduleJobService.pageList(req);
        return ApiResponseUtil.success(scheduleJobPage);
    }

    @PostMapping("runOnce/{jobId}")
    public ApiResponse<Object> runOnce(@PathVariable Long jobId) {
        scheduleJobService.runOnceJob(jobId);
        return ApiResponseUtil.success(true);
    }

    @PostMapping("run/{jobId}")
    public ApiResponse<Object> run(@PathVariable Long jobId) {
        scheduleJobService.start(jobId);
        return ApiResponseUtil.success(true);
    }

    @PostMapping("pause/{jobId}")
    public ApiResponse<Object> pause(@PathVariable Long jobId) {
        scheduleJobService.pause(jobId);
        return ApiResponseUtil.success(true);
    }

    @GetMapping("log/pageList")
    public ApiResponse<Pagination<ScheduleJobLog>> logPageList(ScheduleJobLogReq req) {
        Page<ScheduleJobLog> scheduleJobLogPage = scheduleJobLogService.pageList(req);
        return ApiResponseUtil.success(scheduleJobLogPage);
    }
}
